/*
 * create by lixy 20150323
 * Just for DMap(tuteng) Project
 * copyright@ 2015
 */
"use strict";

var app = angular.module('manager.pad',
    ['ngRoute', 'ngSanitize',
    'ui.router',
    'angular-timeline',
    'angular-scroll-animate']);

//app.config(['$routeProvider', function ($routeProvider) {
//    $routeProvider
//        .when('/login', {
//            templateUrl: 'page/login.html',
//            controller: 'loginController'
//        })
//        .when('/register', {
//            templateUrl: 'page/register.html',
//            controller: 'registerController'
//        })
//        // 功能点大圆
//        .when('/', {
//            templateUrl: 'index.html',
//            controller: 'indexController'
//        })
//        .otherwise({
//            redirectTo: '/'
//        });
//}]);

/* Controllers */
app.controller('indexController',  ['$scope', '$location', '$rootScope', '$routeParams','indexService', function ($scope, $location, $rootScope, $routeParams, indexService) {
    var vm = $scope.vm = {};
    // 调整到指定页面
    window.location.href= "/star/index.html";
}]);

/* Services */
app.service('indexService',['ajaxService', function (ajaxService) {
    this.list = function (params, successFunction, errorFunction) {
        ajaxService.AjaxGetWithData(params, "./api/v1/bibcp/xxx/list", successFunction, errorFunction);
    };
    this.ping = function(params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "./api/v1/bibcp/aaa/ping", successFunction, errorFunction)
    };
}]);

app.filter('contentFmt1', function() {
    return function(input) { // input是我们传入的字符串
        var strs=input.split(",");
        return (strs.length>1)?strs[0]:input;
    }
});
app.filter('contentFmt2', function() {
    return function(input) { // input是我们传入的字符串
        var strs=input.split(",");
        return (strs.length>1)?strs[1]:input;
    }
});


function strFormatter(value, row, index) {
    if(value==null) return null;
    return value+"";
}

function resultStatusFormatter(value, row, index) {
    if(value==null) return null;
    var ret = '';
    if(value==0) {
        ret = '正常';//formatColorCtx('新增','blue');
    }else if(value==1){
        ret = '变更';//formatColorCtx('变更','green');//yellow
    }else {
        ret = formatColorCtx('无效:'+value,'red');
    }
    return ret;
}
function orderStatusFormatter(value, row, index) {
    if(value==null) return null;
    var ret = '';
    if(value==20) {
        ret = '待结账';//formatColorCtx('堂食','blue');
    }else if(value==30){
        ret = '已作废';//formatColorCtx('外卖','green');
    }else if(value==40){
        ret = '已结账';//formatColorCtx('自提','yellow');
    }else {
        ret = formatColorCtx('未知:'+value,'red');
    }
    return ret;
}

function orderSubTypeFormatter(value, row, index) {
    if(value==null) return null;
    var ret = '';
    if(value==0) {
        ret = '堂食';//formatColorCtx('堂食','blue');
    }else if(value==20){
        ret = '外卖';//formatColorCtx('外卖','green');
    }else if(value==21){
        ret = '自提';//formatColorCtx('自提','yellow');
    }else {
        ret = formatColorCtx('未知:'+value,'red');
    }
    return ret;
}

function netOrderTypeCodeFormatter(value, row, index) {
    if(value==null) return null;
    var ret = '';
    var temp = value;
    if(value.length>5) {
        temp = value.substr(0,5);
    }
    if(temp=='WS_YD') {
        ret = '网上预订';//formatColorCtx('网上预订','blue');
    }else if(temp=='WS_SC'){
        ret = '网上闪吃';//formatColorCtx('网上闪吃','blue');
    }else if(temp=='WS_WM'){
        ret = '网上外卖';//formatColorCtx('网上外卖','yellow');
    }else if(temp=='WS_ZT'){
        ret = '网上自提';//formatColorCtx('网上自提','yellow');
    }else if(temp=='WS_DN'){
        ret = '堂食自助';//formatColorCtx('堂食自助','blue');
    }else {
        ret = formatColorCtx('未知:'+value,'red');
    }
    return ret;
}

function reasonFormatter(value, row, index) {
    if(value==null) return null;
    return formatColorCtx(value,'red');
}



function checkStatusFormatter(value, row, index) {
    if(value==null) return null;
    var ret = '';
    if(value=='NORMAL') {
        ret = formatColorCtx('正常:'+value,'blue');
    }else if(value=='ABNORMAL_REDIS_EXTRA_DATA'){
        ret = formatColorCtx('异常:REDIS数据未删除','red');
    }else if(value=='ABNORMAL_REDIS_NO_DATA'){
        ret = formatColorCtx('异常:REDIS无计算结果','red');
    }else if(value=='ABNORMAL_HBASE_NO_DATA'){
        ret = formatColorCtx('异常:HBASE缺少记录','red');
    }else {
        ret = formatColorCtx('未知:'+value,'yellow');
    }
    return ret;
}

app.filter('checkReasonIDFormatter', function() {
    return function(value) { // value是我们传入的字符串
        return checkReasonIDFormatter(value,null,null);
    }
});
app.filter('hllDtFormatter', function() {
    return function(value) { // value是我们传入的字符串
        return hll2StdTime(value);
    }
});

function colorByVal(val) {
    var color = "green";
    if (val >= 90) {
        color = "red";
    } else if (val >= 80) {
        color = "yellow";
    }
    return color;
}
function formatColor(value) {
    var color = colorByVal(value);
    return ['<font color="',color,'">',value,'</font>'].join('');
}
function formatColorCtx(value,color) {
    return ['<font color="',color,'">',value,'</font>'].join('');
}

// 异常ID可视化
function checkReasonIDFormatter(value, row, index) {
    if(value==null) return null;
    return [value,'-',reasonName(value)].join('');
}

function recordCountCodeFormatter(value, row, index) {
    if(value==null) return null;
    var ret = '';
    var temp = value;
    if(value.length>4) {
        temp = value.substr(0,4);
    }
    if(temp=='B001') {
        ret = '有效账单';
    }else if(temp=='X001'){
        ret = '总订单';
    }else if(temp=='X002'){
        ret = '线上订单';
    }else if(temp=='C001'){
        ret = '会员V1';
    }else if(temp=='C002'){
        ret = '会员V2';
    }else {
        ret = value;
    }
    return ret;
}

function contextFormatter(value, row, index) {
    if(value==null) return null;
    var ret = '';
    var len = value.length;
    var step = 80;
    for(var i=0;i<len;i+=step) {
        ret += value.substr(i,step);
        ret += "<br/>"
    }
    return ret;
}
function orderKeyFormatter(value, row, index) {// /profiling/:groupID/:shopID/:saasOrderID
    return ['<a href="#/profiling/',row.groupID,'/',row.shopID,'/',value,'" target="_blank">', value, '</a>'].join('');
}
function reasonTypeFormatter(value, row, index) {// /profiling/:groupID/:shopID/:saasOrderID
    return [value,'<a href="http://help.hualala.com/pages/viewpage.action?pageId=3146289" target="_blank">帮助</a>'].join('');
}

function getTsBefore(gap){
    if(gap==null || gap==0) gap=0;
    var today=new Date();
    var before=new Date();
    before.setTime(today.getTime()-1000*gap);
    var strYear=before.getFullYear();
    var strDay=before.getDate();
    if(strDay<10) {
        strDay = "0"+strDay;
    }
    var strMonth=before.getMonth()+1;
    if(strMonth<10) {
        strMonth="0"+strMonth;
    }
    var strHour=before.getHours();
    if(strHour<10) {
        strHour="0"+strHour;
    }
    var strMin=before.getMinutes();
    if(strMin<10) {
        strMin="0"+strMin;
    }
    var strSec=before.getSeconds();
    if(strSec<10) {
        strSec="0"+strSec;
    }
    return (""+strYear+strMonth+strDay+strHour+strMin+strSec);
}

function getHMBefore(gap){
    if(gap==null || gap==0) gap=0;
    var today=new Date();
    var before=new Date();
    before.setTime(today.getTime()-1000*60*gap);
    var strYear=before.getFullYear();
    var strDay=before.getDate();
    if(strDay<10) {
        strDay = "0"+strDay;
    }
    var strMonth=before.getMonth()+1;
    if(strMonth<10) {
        strMonth="0"+strMonth;
    }
    var strHour=before.getHours();
    if(strHour<10) {
        strHour="0"+strHour;
    }
    var strMin=before.getMinutes();
    if(strMin<10) {
        strMin="0"+strMin;
    }
    return (""+strYear+strMonth+strDay+strHour+strMin);
}
function getHBefore(gap){
    if(gap==null || gap==0) gap=0;
    var today=new Date();
    var before=new Date();
    before.setTime(today.getTime()-1000*60*60*gap);
    var strYear=before.getFullYear();
    var strDay=before.getDate();
    if(strDay<10) {
        strDay = "0"+strDay;
    }
    var strMonth=before.getMonth()+1;
    if(strMonth<10) {
        strMonth="0"+strMonth;
    }
    var strHour=before.getHours();
    if(strHour<10) {
        strHour="0"+strHour;
    }
    return (""+strYear+strMonth+strDay+strHour);
}
function getHMDefault(gap){
    if(gap==null || gap==0) gap=0;
    var today=new Date();
    var before=new Date();
    before.setTime(today.getTime()-1000*60*60*gap);
    var strYear=before.getFullYear();
    var strDay=before.getDate();
    if(strDay<10) {
        strDay = "0"+strDay;
    }
    var strMonth=before.getMonth()+1;
    if(strMonth<10) {
        strMonth="0"+strMonth;
    }
    return (""+strYear+strMonth+strDay+"0000");
}
//长字符串表格截取显示
function longContentFmt(value, row, index) {
    if (value.length > 60) {
        // $("#message-text").val(value);
        return value.substring(0,60)+"..."+"<a data-toggle=\"modal\" data-target=\"#exampleModal\" onclick=\"showLongContent("+index+")\" href=\"javascript::void(0)\">[详情]</a>";
    } else {
        return "<div style=\"font-family:'微软雅黑'\">" + value + "</div>";
    }
}
//使用弹框显示所有内容
function showLongContent(index){
    var element  = angular.element($("#message-text"));
    var scope = element.scope();
    scope.$apply(function(){
        scope.index=index;
    });
}

function delete_confirm()
{
     return confirm("删除是不可恢复的，你确认要删除吗？");
}
function numberFmt(value, row, index){
    if(!value) return '';
    return value.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,'$&,');
}

function add0(m){return m<10?'0'+m:m }
function dateFormat(value, row, index) {
//shijianchuo是整数，否则要parseInt转换
    var time = new Date(value);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
function hll2StdTimeFmt(value, row, index) {
    // var sourcesize = {data:value};
    return hll2StdTime(value);
}
